package reference;

import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

public class WeakReferenceDemo {
	public static void main(String[] args) {
		//test();
		clear();
		// Object o = new Object();
		// WeakReference<Object> weakCar = new WeakReference<Object>(o);
		// int i = 0;
		// while (true) {
		// if(i%1000==0){
		// System.gc();
		// }
		// if (weakCar.get() != null) {
		// i++;
		// System.out.println("Object is alive for " + i + " loops - " + weakCar);
		// } else {
		// System.out.println("Object has been collected.");
		// break;
		// }
		// }
	}

	public static void test() {

		// 没有引用了，然后就可以被回收
		Object o = new Object();
		WeakReference<Object> ref = new WeakReference<Object>(o);
		System.out.println(ref.get());
		o = null;
		System.gc();
		// System.gc();
		System.out.println(ref.get());
		//System.out.println(o);
	}
	
	public static void clear(){
		Object o = new Object();
		WeakReference<Object> ref = new WeakReference<Object>(o);
		ref.clear();
		System.out.println(ref.get());
	}
}
